package cn.signalpha.modules.social.wrapper;

import cn.signalpha.common.cache.CacheNames;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.core.mp.support.BaseEntityWrapper;
import cn.signalpha.core.redis.cache.RedisCache;
import cn.signalpha.core.tool.jackson.JsonUtil;
import cn.signalpha.core.tool.utils.*;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.shop.service.GoodsService;
import cn.signalpha.modules.shop.vo.OrderGoodsVO;
import cn.signalpha.modules.social.entity.Topic;
import cn.signalpha.modules.social.service.CommentService;
import cn.signalpha.modules.social.service.FansService;
import cn.signalpha.modules.social.service.LikesService;
import cn.signalpha.modules.social.service.TopicService;
import cn.signalpha.modules.social.vo.TopicVO;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.time.Duration;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 */
public class TopicWrapper extends BaseEntityWrapper<Topic, TopicVO> {

	private static final RedisCache redisCache;
	private static final UserService userService;
	private static final FansService fansService;
	private static final TopicService topicService;
	private static final LikesService likesService;
	private static final GoodsService goodsService;
	private static final CommentService commentService;

	static {
		redisCache = SpringUtil.getBean(RedisCache.class);
		userService = SpringUtil.getBean(UserService.class);
		fansService = SpringUtil.getBean(FansService.class);
		topicService = SpringUtil.getBean(TopicService.class);
		likesService = SpringUtil.getBean(LikesService.class);
		goodsService = SpringUtil.getBean(GoodsService.class);
		commentService = SpringUtil.getBean(CommentService.class);
	}

	public static TopicWrapper build() {
		return new TopicWrapper();
	}

	@Override
	public TopicVO entityVO(Topic topic) {
		TopicVO topicVO = Objects.requireNonNull(BeanUtil.copy(topic, TopicVO.class), MsgConstant.ACCOUNT_NULL);

		User user = userService.getById(topicVO.getUserId());
		if(ObjectUtil.isNotEmpty(user)){
			topicVO.setUserAvatar(user.getAvatar());
			topicVO.setUserNickName(user.getNickName());
		}

		topicVO.setGoodsList(JsonUtil.parseArray(topicVO.getTarget(), OrderGoodsVO.class));
		return topicVO;
	}

	public TopicVO entityVO(Topic topic, Long userId) {
		TopicVO topicVO = entityVO(topic);
		topicVO.setLikeId(likesService.getIdByUserId(userId, topicVO.getId(), 1));
//		if(ObjectUtil.isNotEmpty(topicVO.getUserId())){
//			topicVO.setIsFans(fansService.isFans(topicVO.getUserId(), userId));
//		}

		//增加点击
		String key = CacheNames.TOPIC_CLICK + userId + topicVO.getId();
		if(StringUtil.isBlank(redisCache.get(key))){
			redisCache.setEx(key, StringPool.ONE, Duration.ofHours(24));
			topicService.updateById(Topic.builder().id(topicVO.getId()).clickNum(topicVO.getClickNum() + 1).build());
		}
		return topicVO;
	}

	public IPage<TopicVO> page(IPage<TopicVO> pages, Long userId) {
		pages.getRecords().forEach(topicVO -> {
			topicVO.setLikeId(likesService.getIdByUserId(userId, topicVO.getId(), 1));
//			if(ObjectUtil.isNotEmpty(topicVO.getUserId())){
//				topicVO.setIsFans(fansService.isFans(topicVO.getUserId(), userId));
//			}
		});
		return pages;
	}

}
